<template>
  <header class="header">
    <div class="container navbar-content">
      <!-- Logo区域 -->
      <div class="title-bar">
        <img class="logo" src="@/assets/logo.jpg" alt="logo" />
        <div class="title">zjmOJ</div>
      </div>

      <!-- 导航菜单 -->
      <nav class="nav-links">
        <a
          v-for="item in visibleRoutes"
          :key="item.path"
          :href="item.path"
          :class="{ active: isActive(item.path) }"
          @click.prevent="doMenuClick(item.path)"
        >
          {{ item.name }}
        </a>
      </nav>

      <!-- 用户信息 -->
      <div class="user-info">
        {{ store.state.user?.loginUser?.userName ?? "未登录" }}
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { routes } from "../router/routes";
import { useRoute, useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";

const router = useRouter();
const route = useRoute();
const store = useStore();

// 展示在菜单的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});

// 判断当前路由是否激活
const isActive = (path: string) => {
  return route.path === path;
};

// 菜单点击事件
const doMenuClick = (path: string) => {
  router.push({ path });
};

// 路由变化时更新状态
router.afterEach(() => {
  // 自动更新激活状态
});
</script>

<style scoped>
/* 导航栏整体样式（与主页一致） */
.header {
  background-color: #f8f9fa;
  border-bottom: 1px solid #eee;
  padding: 0 2rem;
}

.container {
  max-width: 1280px;
  margin: 0 auto;
}

.navbar-content {
  display: flex;
  align-items: center;
  height: 60px; /* 固定高度，与主页一致 */
  gap: 2rem;
}

/* Logo区域样式 */
.title-bar {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo {
  height: 40px; /* 调整logo大小适配导航栏 */
  border-radius: 4px;
}

.title {
  color: #333;
  font-size: 1.5rem;
  font-weight: bold;
}

/* 导航链接样式（与主页一致） */
.nav-links {
  display: flex;
  gap: 1.5rem;
  margin-left: 1rem;
}

.nav-links a {
  color: #333;
  text-decoration: none;
  padding: 0.5rem 0;
  font-size: 1rem;
  position: relative;
}

/* 激活状态样式 */
.nav-links a.active {
  color: #2ecc71;
  font-weight: bold;
}

.nav-links a.active::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #2ecc71;
}

/* 用户信息样式 */
.user-info {
  margin-left: auto;
  color: #333;
  font-size: 0.9rem;
  padding: 0 1rem;
}

/* 响应式调整（与主页一致） */
@media (max-width: 768px) {
  .nav-links {
    display: none; /* 移动端可替换为汉堡菜单 */
  }

  .navbar-content {
    justify-content: space-between;
  }
}
</style>
